﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using System.IO;
using System.Text;
using Entities;

namespace ERP
{
    public partial class ImportOrders : BaseERPPage
    {
        OrderManager orderManager = new OrderManager();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void ButtonUpload_OnClick(object sender, EventArgs e)
        {
            Int32 fileLen = FileUploadToImport.PostedFile.ContentLength;

            // Create a byte array to hold the contents of the file.
            Byte[] buffer = new Byte[fileLen];

            // Initialize the stream to read the uploaded file.
            Stream s = FileUploadToImport.FileContent;

            // Read the file into the byte array.
            s.Read(buffer, 0, fileLen);

            // Convert byte array into characters.
            ASCIIEncoding enc = new ASCIIEncoding();
            string str = enc.GetString(buffer);
            List<CsvError> ErrorsList = orderManager.ImportOrdersFromCSV(str);
            if (ErrorsList.Count == 0)
            {
                LabelSuccesImport.Visible = true;
            }
            else
            {
                foreach (CsvError error in ErrorsList)
                {
                    TextBoxErrors.Text += Environment.NewLine + "Line: " + error.Line + " - " + error.Description;
                }
            }
        }
    }
}